<?php
namespace Client\Controller;

use Think\Controller;

class DiscussController extends Controller{
	//添加评论列表
	public function addDiscuss(){
		 if(IS_POST){
			$data = $_POST;
			$discuss = D('Discuss');
			if($discuss->create($data)){
				$discuss->add();
				$json['result'] = "0";
				$json['msg'] = '添加评论成功';
				$publish = D('publish');
				$pid = I('pid');
				$publish->where(array('id'=>$pid))->setInc("discuss_times",1,60);
			}else{
				$json['result'] = "-1";
				$json['msg'] = $discuss->getError();
			}
			$this->ajaxReturn($json);
		}

	}
	
	//查看评论列表
	public function getDiscuss(){
		//if(IS_POST){
		     $discuss = D('DiscussView');
			 $pid = I('pid');
			if($pid!=null){
				$result = $discuss->where(array('pid'=>$pid))->order(array('ctime'=>'asc'))->select();
//				file_put_contents("Public/Log/log.txt", "post:".json_encode($result)."\r\n",FILE_APPEND|LOCK_EX);
				$count  = M('Discuss')->where(array('pid'=>$pid))->count();
				$publish = M('publish');
				$data['discuss_times'] = $count;
				$publish->where(array('id'=>$pid))->save($data);
				$publish->where(array('id'=>$pid))->setInc("browse_times",1,60);
				foreach($result as $k=>$v){
					$result[$k]['ctime']=date("Y-m-d H:i:s",$v['ctime']);
				}
				$json['result'] = "0";
				$json['count'] = $count;
				$json['msg'] = '成功获取评论列表';
				$json['discuss'] = $result;
			}else{
				$json['result'] = "-1";
				$json['msg'] = "获取约吧id失败";
			}
			$this->ajaxReturn($json);
		//}
	}
	public function myDiscuss(){
		 $discuss = D('DiscussView');
		 $publish = D('PublishView');
		if(IS_POST){
			$uid = $_POST['id'];
			$result = $discuss->where(array('uid'=>$uid))->group('pid')->order(array('ctime'=>'asc'))->select();
			$publishUserInfo=array();
			foreach($result as $k=>$v){
				$result[$k]['ctime']=date("Y-m-d H:i:s",$v['ctime']);
				$userinfo = $publish->where(array('id'=>$result[$k]['pid']))->find();
				array_push($publishUserInfo,$userinfo);
			}
			foreach ($publishUserInfo as $k=>$v){
				$publishUserInfo[$k]['ctime']=date("Y-m-d H:i:s",$v['ctime']);
			}
			if(sizeof($result)>0){
				$json['result'] = "0";
				$json['msg'] = '成功获取评论列表';
				$json['discuss'] = $result;
				$json['publishUserInfo'] =$publishUserInfo; 
			}else{
				$json['result'] = "-1";
				$json['msg'] = "没有更多的评论了";
			}
			$this->ajaxReturn($json);
		}
		else{
			$this->ajaxReturn(array('resule'=>'-2','msg'=>'数据不是以POST方式提交'));
		}
	}
}